package com.linkhub.oms.admin.modules.stock.domain.dto;

import cn.hutool.core.collection.CollUtil;
import com.linkhub.oms.admin.modules.wms.domain.dto.DeliveryListConfirmDTO;
import com.linkhub.oms.common.util.WmsUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.tuple.Pair;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName ConfirmOutboundDTO
 * @Description TODO
 * @Author XieYu
 * @Date 2024/7/7 21:40
 * @Version 1.0
 */
@Data
public class ConfirmOutboundDTO implements Serializable {
    @ApiModelProperty(value = "用户id")
    private String userId;

    @ApiModelProperty(value = "仓库")
    private String stashName;

    @ApiModelProperty(value = "业务类型")
    private String businessType;

    private String businessNo;

    @ApiModelProperty(value = "wms对应单号")
    private String wmsNo;

    @ApiModelProperty(value = "本地商品skuId")
    private String skuId;

    @ApiModelProperty(value = "数量")
    private Integer skuQuantity;

    public static Pair<List<ConfirmOutboundDTO>, List<ConfirmOutboundDTO>> convert(DeliveryListConfirmDTO deliveryListConfirmDTO) {
        List<ConfirmOutboundDTO> confirmOutboundDTOS = Lists.newArrayList();
        if (Objects.nonNull(deliveryListConfirmDTO) && CollUtil.isNotEmpty(deliveryListConfirmDTO.getOrderLines())) {
            for (DeliveryListConfirmDTO.OrderLineDTO orderLineDTO : deliveryListConfirmDTO.getOrderLines()) {
                ConfirmOutboundDTO confirmOutboundDTO = new ConfirmOutboundDTO();
                confirmOutboundDTO.setUserId(WmsUtil.getUserId(orderLineDTO.getItemCode()));
                confirmOutboundDTO.setSkuId(WmsUtil.getSkuId(orderLineDTO.getItemCode()));
                confirmOutboundDTO.setStashName(deliveryListConfirmDTO.getWarehouseCode());
                confirmOutboundDTO.setBusinessType("");
                confirmOutboundDTO.setBusinessNo(deliveryListConfirmDTO.getDeliveryOrderCode());
                confirmOutboundDTO.setSkuQuantity(Integer.parseInt(orderLineDTO.getPlanQty()));
                confirmOutboundDTOS.add(confirmOutboundDTO);
            }
        }
        List<ConfirmOutboundDTO> consumableDTOS = Lists.newArrayList();
        if (Objects.nonNull(deliveryListConfirmDTO) && CollUtil.isNotEmpty(deliveryListConfirmDTO.getConsumableDetails())) {
            for (DeliveryListConfirmDTO.ConsumableDetailDTO consumableDetailDTO : deliveryListConfirmDTO.getConsumableDetails()) {
                ConfirmOutboundDTO confirmOutboundDTO = new ConfirmOutboundDTO();
                confirmOutboundDTO.setUserId(WmsUtil.getUserId(consumableDetailDTO.getConsumableCode()));
                confirmOutboundDTO.setSkuId(WmsUtil.getSkuId(consumableDetailDTO.getConsumableCode()));
                confirmOutboundDTO.setStashName(deliveryListConfirmDTO.getWarehouseCode());
                confirmOutboundDTO.setBusinessType("");
                confirmOutboundDTO.setBusinessNo(deliveryListConfirmDTO.getDeliveryOrderCode());
                confirmOutboundDTO.setSkuQuantity(Integer.parseInt(consumableDetailDTO.getQuantity()));
                consumableDTOS.add(confirmOutboundDTO);
            }
        }
        return Pair.of(confirmOutboundDTOS, consumableDTOS);
    }
}
